package YunNotes.allnotes.dao;

import YunNotes.allnotes.model.NotesPublicSquare;
import YunNotes.util.db.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class PublicSquareAll {
    public ArrayList<NotesPublicSquare> outputPublicSquaresAll() {
        DBUtil obj = new DBUtil();
        Connection conn = DBUtil.getConn();
        ArrayList<NotesPublicSquare> users = new ArrayList<>();
        if (conn == null) {
            System.out.println("数据库连接出错");
            return null;
        }
        String sql = "SELECT * from allnotes" +
                " where publicity = ?";
        try {
            //预编译处理sql语句
            PreparedStatement preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setInt(1, 1);
            ResultSet rs = preparedStatement.executeQuery();
            rs.last();
            rs.beforeFirst();
            while (rs.next()) {
                NotesPublicSquare user = new NotesPublicSquare();
                user.setNum(rs.getInt("num"));
                user.setUserName(rs.getString("user_name"));
                user.setHeadLine(rs.getString("headline"));
                user.setPraisePoints(rs.getInt("praise_points"));
                user.setPageView(rs.getInt("page_view"));
                user.setCreateTime(rs.getTimestamp("create_time"));
                users.add(user);
            }
//                obj.closeConn(conn);
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("SQL语句有误，数据库报错");
            return null;
        }
        return users;
    }


}
